home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / msysjour / vol07 / 02 / controls / mustest.c < prev    next >
C/C++ Source or Header  |  1992-02-29  |  9KB  |  287 lines

  1. /*
  2.  * MUSCROLL.C
  3.  *
  4.  * Contains the main window procedure of the MicroScroll control
  5.  * that handles mouse logic, and Windows messages.
  6.  *
  7.  * Version 1.1, October 1991, Kraig Brockschmidt
  8.  */
  9.  
  10. #include <windows.h>
  11. #include <muscroll.h>
  12. #include "mustest.h"
  13.  
  14.  
  15.  
  16.  
  17. //These are globals for convenience only.
  18. HWND        hVertEdit;
  19. HWND        hHorzEdit;
  20. HWND        hVertMS;
  21. HWND        hHorzMS;
  22.  
  23.  
  24.  
  25. /*
  26.  * WinMain
  27.  *
  28.  * Purpose:
  29.  *  Main entry point of application.   Should register the app class
  30.  *  if a previous instance has not done so and do any other one-time
  31.  *  initializations.
  32.  *
  33.  * Parameters:
  34.  *  Standard
  35.  *
  36.  * Return Value:
  37.  *  int              Value to return to Windows--termination code.
  38.  *
  39.  */
  40.  
  41. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  42.                     LPSTR lpszCmdLine, int nCmdShow)
  43.     {
  44.     WNDCLASS    wc;
  45.     HWND        hWnd;
  46.     MSG         msg;
  47.     TEXTMETRIC  tm;
  48.     HDC         hDC;
  49.     WORD        cxFont;
  50.     WORD        cyFont;
  51.  
  52.  
  53.     if (!hPrevInstance)
  54.         {
  55.         wc.style          = CS_HREDRAW | CS_VREDRAW;
  56.         wc.lpfnWndProc    = MusTestWndProc;
  57.         wc.cbClsExtra     = 0;
  58.         wc.cbWndExtra     = 0;
  59.         wc.hInstance      = hInstance;
  60.         wc.hIcon          = LoadIcon(hInstance, "MusTestIcon");
  61.         wc.hCursor        = LoadCursor(NULL, IDC_ARROW);
  62.         wc.hbrBackground  = COLOR_WINDOW + 1;
  63.         wc.lpszMenuName   = "MusTestMenu";
  64.         wc.lpszClassName  = "MusTest";
  65.  
  66.  
  67.         if (!RegisterClass(&wc))
  68.             return FALSE;
  69.         }
  70.  
  71.  
  72.     //Create and show main window.
  73.     hWnd=CreateWindow("MusTest", "MicroScrolls",
  74.                       WS_MINIMIZEBOX | WS_OVERLAPPEDWINDOW,
  75.                       35, 35, 250, 110,
  76.                       NULL, NULL, hInstance, NULL);
  77.  
  78.     ShowWindow(hWnd, nCmdShow);
  79.     UpdateWindow(hWnd);
  80.  
  81.     //Get text dimensions on which to base control sizes.
  82.     hDC=GetDC(hWnd);
  83.     GetTextMetrics(hDC, &tm);
  84.     cxFont=tm.tmAveCharWidth;
  85.     cyFont=tm.tmHeight;
  86.     ReleaseDC(hWnd, hDC);
  87.  
  88.     //Create a 'spin' button (edit control with vertical MicroScroll
  89.     CreateWindow("static", "Number 1-99:",
  90.                  WS_VISIBLE | WS_CHILD | SS_LEFT,
  91.                  2,   2+(cyFont/4), 16*cxFont, cyFont,
  92.                  hWnd, ID_NULL, hInstance, 0L);
  93.  
  94.     hVertEdit=CreateWindow("edit", "50",
  95.                  WS_VISIBLE | WS_CHILD | ES_LEFT | WS_BORDER,
  96.                  2+16*cxFont, 2, 4*cxFont, (cyFont*3)/2,
  97.                  hWnd, ID_VERTEDIT, hInstance, 0L);
  98.  
  99.     SendMessage(hVertEdit, EM_LIMITTEXT, 2, 0L);
  100.  
  101.     /*
  102.      * Width is forced odd with 1+(2*cxFont) so the small triangle
  103.      * looks perfectly centered.  Simple aesthetics.
  104.      *
  105.      * Parent window is default associate.
  106.      */
  107.     hVertMS=CreateWindow("microscroll", "1,99,50",
  108.                  WS_VISIBLE | WS_CHILD | MSS_SPIN,
  109.                  2+20*cxFont, 2, 1+(2*cxFont), (cyFont*3)/2,
  110.                  hWnd, ID_VERTSCROLL, hInstance, 0L);
  111.  
  112.  
  113.     //Create a horizonal edit control with a horizontal MicroScroll
  114.     hHorzEdit=CreateWindow("edit", "Horizontal Edit Control",
  115.                  WS_VISIBLE | WS_CHILD | ES_LEFT | WS_BORDER,
  116.                  2, 2*cyFont, 30*cxFont, (cyFont*3)/2,
  117.                  hWnd, ID_VERTEDIT, hInstance, 0L);
  118.  
  119.     //Parent window is default associate.
  120.     hHorzMS=CreateWindow("microscroll", "1,99,50",
  121.                  WS_VISIBLE | WS_CHILD | MSS_HORIZONTAL,
  122.                  2+30*cxFont, 2*cyFont, 3*cxFont, (cyFont*3)/2,
  123.                  hWnd, ID_HORZSCROLL, hInstance, 0L);
  124.  
  125.     //Change the horizontal MicroScroll colors to a blue base.
  126.     MSCrColorSet(hHorzMS, MSCOLOR_FACE,      RGB(0,   0,   255));
  127.     MSCrColorSet(hHorzMS, MSCOLOR_ARROW,     RGB(255, 255, 255));
  128.     MSCrColorSet(hHorzMS, MSCOLOR_SHADOW,    RGB(0,   0,   128));
  129.     MSCrColorSet(hHorzMS, MSCOLOR_HIGHLIGHT, RGB(0,   255, 255));
  130.  
  131.     while (GetMessage(&msg, NULL, 0, 0))
  132.         {
  133.         TranslateMessage(&msg);
  134.         DispatchMessage(&msg);
  135.         }
  136.  
  137.     return msg.wParam;
  138.     }
  139.  
  140.  
  141.  
  142. /*
  143.  * MusTestWndProc
  144.  *
  145.  * Purpose:
  146.  *  Window class procedure.  Standard callback.
  147.  *
  148.  * Parameters:
  149.  *  The standard.  See Section 2.4 Windows SDK Guide to Programming,
  150.  *  page 2-4.
  151.  *
  152.  * Return Value:
  153.  *  See Parameters, above.
  154.  *
  155.  */
  156.  
  157. long FAR PASCAL MusTestWndProc(HWND hWnd, unsigned iMessage,
  158.                                WORD wParam, LONG lParam)
  159.     {
  160.     LONG        lPos;
  161.     short       wPos;
  162.     char        szNum[5];
  163.     DWORD       dwRange;
  164.     short       wNum;
  165.     short       wMax;
  166.     short       wMin;
  167.     BOOL        bTranslated;
  168.  
  169.  
  170.     switch (iMessage)
  171.         {
  172.         case WM_DESTROY:
  173.             PostQuitMessage(0);
  174.             break;
  175.  
  176.         case WM_COMMAND:
  177.             switch (wParam)
  178.                 {
  179.                 case IDM_EXIT:
  180.                     PostMessage(hWnd, WM_CLOSE, 0, 0L);
  181.                     break;
  182.                 }
  183.             break;
  184.  
  185.         case WM_HSCROLL:
  186.             //Affect horizontal scrolling in the edit control.
  187.             SetFocus(hHorzEdit);
  188.  
  189.             if (HIWORD(lParam)==hHorzMS)
  190.                 {
  191.                 lPos=SendMessage(hHorzEdit, EM_GETSEL, 0, 0L);
  192.                 wPos=LOWORD(lPos);
  193.  
  194.                 if (wParam==SB_LINEUP)
  195.                     wPos=(--wPos < 0) ? 0 : wPos;
  196.                 else
  197.                     wPos=(++wPos > 32767) ? 32767 : wPos;
  198.  
  199.                 lPos=MAKELONG(wPos, wPos);
  200.                 SendMessage(hHorzEdit, EM_SETSEL, 0, lPos);
  201.                 }
  202.             break;
  203.  
  204.  
  205.         case WM_VSCROLL:
  206.             //For vertical scrolling we implement a spin button.
  207.  
  208.             //Ignore what comes from MSM_WCURRENTPOSSET
  209.             if (wParam==SB_THUMBTRACK)
  210.                 break;
  211.  
  212.             SetFocus(hVertEdit);
  213.  
  214.             if (HIWORD(lParam)==hVertMS)
  215.                 {
  216.                 wPos=LOWORD(lParam);
  217.  
  218.                 /*
  219.                  * The code between here and the wsprintf case where
  220.                  * we change the edit control's text is entirely to
  221.                  * support the case where the use might have typed a
  222.                  * different value into the edit control.
  223.                  */
  224.  
  225.                 dwRange=SendMessage(hVertMS, MSM_DWRANGEGET, 0, 0L);
  226.                 wMax=HIWORD(dwRange);
  227.                 wMin=LOWORD(dwRange);
  228.  
  229.                 //Get the number in the control.
  230.                 wNum=GetDlgItemInt(hWnd, ID_VERTEDIT, &bTranslated, TRUE);
  231.  
  232.  
  233.                 /*
  234.                  * Check if we got a valid value from the control. Otherwise
  235.                  * use the current position.
  236.                  */
  237.                 if (bTranslated && wNum >= 1)
  238.                     {
  239.                     /*
  240.                      * If we are decrementing the value and wNum-1==wPos,
  241.                      * then we don't need to modify anything.  Otherwise,
  242.                      * wPos must become wNum-1 if that is >= minimum.
  243.                      */
  244.                     if (SB_LINEDOWN==wParam && wNum-1!=wPos)
  245.                         {
  246.                         if (wNum > wMin)
  247.                             wPos=wNum-1;
  248.                         }
  249.  
  250.                     if (SB_LINEUP==wParam && wNum+1!=wPos)
  251.                         {
  252.                         if (wNum < wMax)
  253.                             wPos=wNum+1;
  254.                         }
  255.                     }
  256.  
  257.  
  258.                 //Only change the control if we have to.
  259.                 if (wPos!=wNum)
  260.                     {
  261.                     //Convert the value and put it in the control.
  262.                     wsprintf(szNum, "%d", wPos);
  263.                     SetDlgItemText(hWnd, ID_VERTEDIT, szNum);
  264.                     }
  265.  
  266.                 //Update the current position if it changed.
  267.                 if (wPos!=(short)LOWORD(lParam))
  268.                     SendMessage(hVertMS, MSM_WCURRENTPOSSET, wNum, 0L);
  269.  
  270.  
  271.                 /*
  272.                  * We always want to do this in case the user typed something but we
  273.                  * could not scroll (like they typed in the max), in which case the
  274.                  * selection went away.
  275.                  */
  276.                 SendMessage(hVertEdit, EM_SETSEL, 0, MAKELONG(0, 32767));
  277.                 }
  278.             break;
  279.  
  280.  
  281.         default:
  282.             return (DefWindowProc(hWnd, iMessage, wParam, lParam));
  283.         }
  284.  
  285.     return 0L;
  286.     }
  287.